package org.example.Servlet.TopicServlet;

import org.example.Dao.TopicDao;
import org.example.entity.Topic;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/topic/delTopic")
public class DelTopicServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要删除的id
        int tid = Integer.parseInt(req.getParameter("tid"));
        HttpSession session = req.getSession();
        System.out.println("del topic tid ==> " + tid);
        TopicDao topicDao = new TopicDao();
        try {
            //删除
            int count = topicDao.delTopic(tid);
            System.out.println("del topic count ==> " + count);
            if (count > 0) {
                //跟新数据
                List<Topic> topicList = topicDao.getTopicList();
                System.out.println("topicList ==> " + topicList.toString());
                session.setAttribute("topicList", topicList);
                session.setAttribute("delTopicMsg", "");
                req.getRequestDispatcher("/views/admin/index.jsp").forward(req, resp);
            }else {
                session.setAttribute("delTopicMsg", "删除失败");
                req.getRequestDispatcher("/views/admin/index.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            System.out.println("del topic error ==>" + e.getMessage());
            session.setAttribute("delTopicMsg", "删除失败");
            req.getRequestDispatcher("/views/admin/index.jsp").forward(req, resp);
        }

    }
}
